<?php
/**
 * Created by PhpStorm.
 * User: eddy
 * Date: 2019/11/15
 * Time: 23:02
 */

namespace iBrand\Shop\Backend\Service;


use iBrand\Component\Goods\Models\GoodsProduct;
use iBrand\Component\Goods\Models\GoodsSpecRelation;
use iBrand\Component\Goods\Models\SpecsValue;
use iBrand\Component\Goods\Repositories\GoodsProductRepository;
use iBrand\Component\Purchase\Models\Purchase;
use iBrand\Component\Purchase\Repositories\PurchaseRepository;
use iBrand\Shop\Core\Models\Clerk;
use iBrand\Shop\Core\Repositories\O2oGoodsRepository;
use iBrand\Shop\Core\Service\WarehouseService;

class ImportStockService
{
    protected $purchaseRepository;
    protected $productRepository;
    protected $warehouseService;
    protected $o2oGoodsRepository;

    public function __construct(PurchaseRepository $purchaseRepository,
                                GoodsProductRepository $goodsProductRepository,
                                WarehouseService $warehouseService,
                                O2oGoodsRepository $o2oGoodsRepository)
    {
        $this->purchaseRepository = $purchaseRepository;
        $this->productRepository = $goodsProductRepository;
        $this->warehouseService = $warehouseService;
        $this->o2oGoodsRepository = $o2oGoodsRepository;
    }

    public function handleImportStock($data, $shop_id)
    {
        $clerk = Clerk::where('shop_id', $shop_id)->first();
        $purchaseData = [
            'shop_id' => $shop_id,
            'code' => 'JT' . date('YmdHis') . mt_rand(10000000, 99999999),
            'clerk_id' => $clerk->id,
            'type' => 'PURCHASE',
            'remark' => '库存导入',
            'status' => Purchase::STATUS_DEFAULT
        ];

        $purchase = $this->purchaseRepository->create($purchaseData);
        $quantity = 0;
        $total = 0;


        foreach ($data as $item) {
            $sku = $item['sku'];
            $num = $item['quantity'];

            $product = $this->productRepository->getProductBySku($sku);

            if (!$product) continue;

            $goods = $product->goods;

            $item_id = $product->id;
            $item_type = GoodsProduct::class;

            $good['discount'] = 100;
            $good['truePrice'] = $goods->factory_price;
            $good['trueAmount'] = $goods->factory_price * $num;
            $good['quantity'] = $num;
            $good['price'] = $goods->factory_price;
            $good['goods_name'] = $goods->name;
            $good['goods_id'] = $goods->id;
            $good['goods_no'] = $goods->goods_no;
            $good['currentSku'] = $sku;

            $specID = $product->specID;
            $tmpSpec = SpecsValue::find($specID[0]);
            if ($tmpSpec->spec_id == 1) {
                $size = $tmpSpec;
                $color = SpecsValue::find($specID[1]);
            } elseif ($tmpSpec->spec_id == 2) {
                $color = $tmpSpec;
                $size = SpecsValue::find($specID[1]);
            }
            $colorName = GoodsSpecRelation::where('goods_id', $product->goods_id)->where('spec_value_id', $color->id)->first()->alias;
            $good['color'] = $colorName;
            $good['color_id'] = $color->id;
            $good['size'] = $size->name;
            $good['size_id'] = $size->id;

            $this->warehouseService->addPurchaseItem($purchase->id, $good, $item_id, $item_type);

            $this->warehouseService->addStockRecord($shop_id, $purchase->id, $good, 'PURCHASE', $item_id, $item_type);

            $o2oGoods = $this->o2oGoodsRepository->findWhere(['status' => 1, 'goods_id' => $good['goods_id'], 'shop_id' => $shop_id])->first();
            if (!$o2oGoods) {
                $o2oGoods = $this->o2oGoodsRepository->create([
                    'goods_id' => $good['goods_id'],
                    'shop_id' => $shop_id,
                    'price' => $good['price'],
                ]);
            }

            $o2oGoodsId = $o2oGoods->id;

            $this->warehouseService->addO2oGoodsProduct($shop_id, $o2oGoodsId, $good['goods_id'], $good['quantity'], 'Purchase', 'PURCHASE', $item_id);

            $o2oGoods->restoreStock($good['quantity']);

            $quantity += $good['quantity'];
            $total += $good['quantity'] * $good['price'];

        }

        $purchase->quantity = $quantity;
        $purchase->total = $total;
        $purchase->save();
    }
}